require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
require 'win32ole'
require 'win32/process'

class TC_MediaGroup_test < Test::Unit::TestCase
   
    def startup
      $GroupName= 'MediaGroup'
      $GroupNameEdit = $GroupName +'-Edit'
      $GroupDescription= 'This is the first media group for CS 2008'
      $GroupDescriptionEdit =$GroupDescription+'-Edit'
      $GalleryName = 'Gallery1'
      $GalleryNameEdit = $GalleryName+'-Edit'
      $AppKey = 'MediaAppKey1'
      $AppKeyEdit = $AppKey+'-Edit'
      $Owners = 'admin'
      $NameRole = 'Registered Users'
      $FileCountQuota ='20'
      $TotalStorageQuota ='25000000'
      $Allowed = 'zip,jpg,jpeg,png,bmp,xml'
      $Restricted ='gif,txt'
      @@ie = Watir::IE.start($SITE_URL)
      
     
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_0000
        require 'win32/process'
        $pid = Process.create(
        :app_name      => 'ruby ../resources/clickers.rb',
        :creation_flags  => Process::DETACHED_PROCESS
        ).process_id
     
    end  

    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
   def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
      end
      
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
      end
      
    
    def test_08_navigateToMediaAdministration
        ie.goto($SITE_URL + 'controlpanel/MediaGalleryAdmin/')
        assert(ie.contains_text('Media Gallery Administration'))      
    end  
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Media Gallery Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
      end
      
    def test_10_navigateToCreateMediaGroup 
        ie.goto($SITE_URL +'controlpanel/MediaGalleryAdmin/Groups.aspx')
        assert(ie.button(:value,'Add New Group').exists?)
        ie.button(:value,'Add New Group').click
    end
    def test_11_navigateToCreateGroup
        assert(ie.text_field(:id,/GroupName$/).exists?)
        ie.text_field(:id,/GroupName$/).value =$GroupName
        assert(ie.text_field(:id,/GroupDesc$/).exists?)
        ie.text_field(:id,/GroupDesc$/).value =$GroupDescription
        assert(ie.button(:id,/Save$/).exists?)
        ie.button(:id,/Save$/).click
    end
    
    def test_13_navigateToEditMediaGroup
        sleep 5
        ie.goto($SITE_URL+'controlpanel/MediaGalleryAdmin/Groups.aspx')
        assert(ie.table(:id,'GroupLists').exists?)
        table1 = ie.table(:id,'GroupLists')
        rows = table1.row_count
        rowcount1 =1
        while rowcount1<=rows
            if (table1[rowcount1][1].link(:text,$GroupName).exists?)
               table1[rowcount1][3].button(:value ,'Edit').click
                break
             end   
         rowcount1=rowcount1+1
        end
        sleep 3
        assert(ie.text_field(:id,/GroupName$/).exists?)
        sleep 2
        ie.text_field(:id,/GroupName$/).value =$GroupNameEdit
        sleep 2
        assert(ie.text_field(:id,/GroupDesc$/).exists?)
        ie.text_field(:id,/GroupDesc$/).value =$GroupDescriptionEdit
        sleep 2
        assert(ie.button(:id,'ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_Save').exists?)
        ie.button(:id,'ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_Save').click
        sleep 5
      # Permissions
        ie.goto($SITE_URL+'controlpanel/MediaGalleryAdmin/Groups.aspx')
        assert(ie.table(:id,'GroupLists').exists?)
        table1 = ie.table(:id,'GroupLists')
        rows = table1.row_count
        rowcount1 =1
        while rowcount1<=rows
            if (table1[rowcount1][1].link(:text,$GroupNameEdit).exists?)
               table1[rowcount1][3].button(:value ,'Edit').click
                break
             end   
         rowcount1=rowcount1+1
        end
        sleep 5
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_ctl01.GetTabAtIndex(1))")
        assert(ie.select_list(:id,/UnchangedPermissionsList$/).exists?)
        ie.select_list(:id,/UnchangedPermissionsList$/).select($NameRole)
        assert(ie.button(:id,/AddUserPermissionButton$/).exists?)
        ie.button(:id,/AddUserPermissionButton$/).click
        assert(ie.table(:class,'Grid').exists?)
        table1 = ie.table(:class,'Grid')
        rows = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rows
            if (table1[rowcount1][1].text == $NameRole)
              ok =1
               sleep 5
               table1[rowcount1][5].checkbox(:id,/EditCheckbox$/).set
               break
            end   
         rowcount1=rowcount1+1
        end
        sleep 3
        assert(ie.button(:id,'ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_Save').exists?)
        sleep 2
        ie.button(:id,'ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_Save').click
        sleep 5
    end
    def test_14_navigateToCreateMediaGallery
        ie.goto($SITE_URL +'controlpanel/MediaGalleryAdmin/CreateEditMediaGallery.aspx')
        assert(ie.text_field(:id,/Name$/).exists?)
        ie.text_field(:id,/Name$/).value =$GalleryName
        assert(ie.text_field(:id,/AppKey$/).exists?)
        ie.text_field(:id,/AppKey$/).value = $AppKey
        $key1 =$AppKey.downcase
        assert(ie.select_list(:id,/AdminGroupList$/).exists?)
        sleep 5
        ie.select_list(:id,/AdminGroupList$/).select($GroupNameEdit)
        sleep 5
        assert(ie.radio(:id,/ynIsActive_0$/).exists?)
        assert(ie.radio(:id,/ynIsActive_1$/).exists?)
        ie.radio(:id,/ynIsActive_0$/).set
        assert(ie.radio(:id,/ynIsSearchable_0$/).exists?)
        assert(ie.radio(:id,/ynIsSearchable_1$/).exists?)
        ie.radio(:id,/ynIsSearchable_0$/).set
        assert(ie.text_field(:id,/Owners_ctl00$/).exists?)
        ie.text_field(:id,/Owners_ctl00$/).value =$Owners
        assert(ie.select_list(:id,/DefaultLanguage$/).exists?)
        ie.select_list(:id,/DefaultLanguage$/).select('Chinese (Traditional)')
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
        sleep 5
    end
      
    def test_15_EditGallery
        ie.goto($SITE_URL+'controlpanel/MediaGalleryAdmin/MediaGalleries.aspx')
        assert(ie.table(:id,'BlogLists').exists?)
        table1 = ie.table(:id,'BlogLists')
        rows = table1.row_count
        rowcount1 =1
        while rowcount1<=rows
            if (table1[rowcount1][1].link(:text,$key1).exists?)
               table1[rowcount1][6].button(:value ,'Edit').click
                break
             end   
         rowcount1=rowcount1+1
        end
        sleep 3
        assert(ie.text_field(:id,/Name$/).exists?)
        ie.text_field(:id,/Name$/).value =$GalleryNameEdit
        assert(ie.text_field(:id,/AppKey$/).exists?)
        ie.text_field(:id,/AppKey$/).value =$AppKeyEdit
        $AppKey1 = $AppKeyEdit.downcase
        assert(ie.text_field(:id,/Owners_ctl00$/).exists?)
        ie.text_field(:id,/Owners_ctl00$/).value =$Owners
        assert(ie.select_list(:id,/AdminGroupList$/).exists?)
        ie.select_list(:id,/AdminGroupList$/).select($GroupNameEdit)
        assert(ie.radio(:id,/ynIsActive_1$/).exists?)
        ie.radio(:id,/ynIsActive_1$/).set
        sleep 1
        assert(ie.radio(:id,/ynIsActive_0$/).exists?)
        ie.radio(:id,/ynIsActive_0$/).set
        assert(ie.radio(:id,/ynIsSearchable_1$/).exists?)
        ie.radio(:id,/ynIsSearchable_1$/).set
        assert(ie.select_list(:id,/SectionDetails1_DefaultLanguage$/).exists?)
        ie.select_list(:id,/SectionDetails1_DefaultLanguage$/).select('Greek')
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
        sleep 2
        # Options
        ie.goto($SITE_URL+'controlpanel/MediaGalleryAdmin/MediaGalleries.aspx')
        assert(ie.link(:text,'All').exists?)
        ie.link(:text,'All').click
        assert(ie.table(:id,'BlogLists').exists?)
        table1 = ie.table(:id,'BlogLists')
        rows = table1.row_count
        rowcount1 =1
        while rowcount1<=rows
            if (table1[rowcount1][1].link(:text,$AppKey1).exists?)
               table1[rowcount1][6].button(:value ,'Edit').click
                break
             end   
         rowcount1=rowcount1+1
        end
        sleep 3
        sleep 2
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_MediaGalleryDetails_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_MediaGalleryDetails_EditorTabs.GetTabAtIndex(1))")
        sleep 2
        assert(ie.radio(:id,/EnableExternalLinks_0$/).exists?)
        ie.radio(:id,/EnableExternalLinks_0$/).set
        assert(ie.radio(:id,/EnableDownloadDisclaimer_0$/).exists?)
        ie.radio(:id,/EnableDownloadDisclaimer_0$/).set
        sleep 2
        assert(ie.text_field(:id,/AllowedFileExtensions$/).exists?)
        sleep 4
        ie.text_field(:id,/AllowedFileExtensions$/).value =$Allowed
        sleep 4
        assert(ie.text_field(:id,/RestrictedFileExtensions$/).exists?)
        sleep 4
        ie.text_field(:id,/RestrictedFileExtensions$/).value =$Restricted
        # Permissions
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_MediaGalleryDetails_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_MediaGalleryDetails_EditorTabs.GetTabAtIndex(2))")
        assert(ie.select_list(:id,/UnchangedPermissionsList$/).exists?)
        ie.select_list(:id,/UnchangedPermissionsList$/).select($NameRole)
        assert(ie.button(:id,/AddUserPermissionButton$/).exists?)
        ie.button(:id,/AddUserPermissionButton$/).click
        sleep 3
        assert(ie.table(:index,3).exists?)
        table1 = ie.table(:index,3)
        rows = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rows
            if (table1[rowcount1][1].text == $NameRole)
              ok =1
               sleep 5
               table1[rowcount1][3].checkbox(:id,/ReadCheckbox$/).set
               break
            end   
         rowcount1=rowcount1+1
        end 
      # Quota
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_MediaGalleryDetails_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_MediaGalleryDetails_EditorTabs.GetTabAtIndex(3))")
        assert(ie.text_field(:id,/FileCountQuota$/).exists?)
        ie.text_field(:id,/FileCountQuota$/).value =$FileCountQuota
        assert(ie.text_field(:id,/TotalStorageQuota$/).exists?)
        ie.text_field(:id,/TotalStorageQuota$/).value =$TotalStorageQuota
        assert(ie.button(:id,/SaveButton$/).exists?)
        ie.button(:id,/SaveButton$/).click
        sleep 3
    end  
    def test_16_DeleteMediaGallery
        ie.goto($SITE_URL+'controlpanel/MediaGalleryAdmin/MediaGalleries.aspx')
        sleep 2
        assert(ie.link(:text,'All').exists?)
        ie.link(:text,'All').click
        sleep 2
        assert(ie.table(:id,'BlogLists').exists?)
        table1 = ie.table(:id,'BlogLists')
        rowcount = table1.row_count
        rowcount1 =1
        ok = 0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$AppKey1).exists?)
              ok =1
               sleep 5
               table1[rowcount1][6].button(:value,'Delete' ).click
               break
            end   
         rowcount1=rowcount1+1
        end
      end 
    def test_17_DeleteMediaGroup
        ie.goto($SITE_URL+'controlpanel/MediaGalleryAdmin/Groups.aspx')
        assert(ie.table(:id,'GroupLists').exists?)
        table2 = ie.table(:id,'GroupLists')
        rows = table2.row_count
        rowcount2 =1
        ok = 0
        while rowcount2<=rows
            if (table2[rowcount2][1].link(:text,$GroupNameEdit).exists?)
              ok =1
               sleep 5
               table2[rowcount2][3].button(:value,'Delete' ).click
               break
            end   
         rowcount2=rowcount2+1
        end
      end
      
    def test_99_Exit
        ie.close
    end
 
end